package com.Lgwd.controller.userController;

import com.Lgwd.dao.UserDao;
import com.Lgwd.entity.UserAccountEntiy;
import com.Lgwd.entity.UserEntiy;
import com.Lgwd.service.userService.UserService;
import com.Lgwd.utils.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@CrossOrigin
public class UserController {


    @Autowired
    UserService userService;

    /**
     * 用户登录获取角色信息
     * <p>
     * 该信息被调用后 持久保存到前端缓存中  生成视图
     */
    @RequestMapping("/loginByUserInfo")
    public ResponseData loginByUserInfo(@RequestBody UserAccountEntiy userAccountEntiy) {
        log.info("【登入接口接收信息】：" + userAccountEntiy.toString());
        UserEntiy userEntiy = userService.findUserInfo(userAccountEntiy);
        if (userEntiy != null) {
            userEntiy.setUser_pwd("");
            return ResponseData.defaultSuccess(userEntiy);
        }

        return ResponseData.defaultFail("账号不存在");
    }


    /**
     * 击杀后提供经验
     * 返回获取经验后的数据
     */
    @RequestMapping("/giveEx")
    public ResponseData giveEx(@RequestBody UserEntiy userEntiy) {

        log.info("【提供" + userEntiy.getUser_ex() + "经验】到【" + userEntiy.getUser_account() + "账户下】");
        //更新数据库 该角色的经验值 并返回最新的数据
        UserEntiy user = userService.findUserGiveUp(userEntiy);
        return ResponseData.defaultSuccess(user);
    }


    /**
     * 角色升级功能 对应前端按钮
     *
     * @param userEntiy
     * @return
     */
    @RequestMapping("/updateUserLv")
    public ResponseData updateUserLv(@RequestBody UserEntiy userEntiy) {
        UserEntiy user = userService.updateUserLv(userEntiy);
        if(user!=null){
            log.info("【用户" + user.getUser_account() + "】【提升到了[Lv:" + user.getUser_lv() + "】");
            return ResponseData.defaultSuccess(user);
        }
      return ResponseData.defaultFail("升级经验不足");
    }


    /**
     * 查询角色属性功能
     */
    @RequestMapping("/getUserProperty")
    public ResponseData getUserProperty(@RequestBody UserDao userDao) {
        UserEntiy userEntiy = userService.findUserInfoByAccount(userDao);
        return ResponseData.defaultSuccess(userEntiy);
    }
}
